﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MoreEPG.Model;
using MoreEPG.Helper;
using System.IO;


namespace MoreEPG
{
    public class HelperClass
    {
        

        /// <summary>
        /// Get an Array of Persons from String (Clickfinder format)
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        public static Person[] getArrayOfPersons(string person)
        {
            //Regex expr = new Regex(@"\((\w+\s?\w*)\)");
            Regex expr = new Regex(@"\((.*)\)");
            ArrayList ret = new ArrayList();
            
            string[] split = person.Split(";".ToCharArray());
            foreach (string p in split)
            {
                Match m = expr.Match(p);
                if (m.Success)
                {
                    Person pers = new Person(ret.Count + 1, m.Result("$`").Replace(@"&", "u.").Trim());
                    string _match = m.Result("$1").Trim();
                    if (_match == "Moderation")
                    {
                        pers.Role = Roles.HostRole;
                    }
                    else if (_match == "Drehbuch")
                    {
                        pers.Role = Roles.WriterRole;
                    }
                    else if (_match == "Musik")
                    {
                        continue;
                    }
                    else if (_match == "Kamera")
                    {
                        continue;
                    }
                    else if (_match == "Gast")
                    {
                        continue;
                    }
                    else
                    {
                        pers.Role = Roles.ActorRole;
                        pers.name = pers.name;
                        pers.Rolle = _match.Replace(@"&", "u.");
                    }
                    ret.Add(pers);
                }
            }

            return (Person[])ret.ToArray(typeof(Person));
        }

        /// <summary>
        /// G:"General Audiences-All Ages Admitted."  ====> FSK 0
        /// PG:"Parental Guidance Suggested. Some Material May Not Be Suitable For Children."   ====> FSK 6
        /// PG-13:"Parents Strongly Cautioned. Some Material May Be Inappropriate For Children Under 13."  ====> FSK 12
        /// R:"Restricted, Under 17 Requires Accompanying Parent Or Adult Guardian."  ====> FSK 16
        /// NC-17:"No One 17 And Under Admitted."  ====> FSK 18
        /// </summary>
        /// <param name="Fsk"></param>
        /// <returns></returns>
        public static mpaaRating FskToMpaa(int Fsk)
        {
            mpaaRating ret = new mpaaRating();
            switch (Fsk)
            {
                case 0:
                    ret = mpaaRating.G;
                    break;
                case 6:
                    ret = mpaaRating.PG;
                    break;
                case 12:
                    ret = mpaaRating.PG13;
                    break;
                case 16:
                    ret = mpaaRating.R;
                    break;
                case 18:
                    ret = mpaaRating.NC17;
                    break;
                default:
                    ret = mpaaRating.Unknown;
                    break;
            }
            return ret;
        }

        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsWow64Process([In] IntPtr hProcess, [Out]out bool lpSystemInfo);

        public static bool Is64Bit()
        {
            if (IntPtr.Size == 8 || (IntPtr.Size == 4 && Is32BitProcessOn64BitProcessor()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool Is32BitProcessOn64BitProcessor()
        {
            bool retVal;
            IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
            return retVal;
        }

        public static int GetHashCode(string Title, string ShortDescription)
        {
            if (string.IsNullOrEmpty(Title) || string.IsNullOrEmpty(ShortDescription))
            {
                throw new ApplicationException("Title and / or ShortDecription is Null or Empty!");
            }
            string str = Title + " " + ShortDescription;
            return str.GetHashCode();
        }

        public static int GetHashCode(string hashString)
        {
            if (string.IsNullOrEmpty(hashString))
            {
                throw new ApplicationException("HashString is Null or Empty!");
            }

            return hashString.GetHashCode();
        }

        public static int GetPercentage(int counter, int count)
        {
            return (int)(((decimal)counter / (decimal)count) * 100);
        }

        public static string Ehome_Path = Environment.GetEnvironmentVariable("windir") + @"\ehome\";
        public static string schTask = Environment.GetEnvironmentVariable("windir") + @"\system32\schtasks.exe";
        public static string appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
        public static string tmpPath
        {
            get
            {
                string ret = appStartPath + @"\tmp\";
                if (!Directory.Exists(ret))
                {
                    Directory.CreateDirectory(ret);
                }
                return ret;
            }
        }
        public static string confPath
        {
            get
            {
                string ret = appStartPath + @"\conf\";
                if (!Directory.Exists(ret))
                {
                    Directory.CreateDirectory(ret);
                }
                return ret;
            }
        }

        public static bool isW7
        {
            get
            {
                PlatformID ID = Environment.OSVersion.Platform;
                int major = Environment.OSVersion.Version.Major;
                int minor = Environment.OSVersion.Version.Minor;
                if (ID == PlatformID.Win32NT && major == 6 && minor == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
